﻿package org.catapult.eslide.module {
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.system.SecurityPanel;
	import flash.utils.Timer;
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.core.UIComponent;
	import mx.effects.Fade;
	import mx.effects.Parallel;
	import mx.events.EffectEvent;
	import org.catapult.eslide.model.GalleryItem;
	
	import mx.containers.Panel;
	
	/**
	 * ...
	 * @author Lam Do
	 */
	public class GalleryCanvas extends Canvas{
		[Bindable]
		private var _xml:XML;
		
		public var faceInBitmap: Bitmap;
		public var faceOutBitmap: Bitmap;
		
		[Bindable]
		public var upperUIComponent: UIComponent;
		[Bindable]
		public var lowerUIComponent: UIComponent;
		
		public var mainEffect: Parallel;
		public var thumbEffect: Parallel;
		
		public var fadeIn: Fade;
		public var fadeOut: Fade;
		
		private var _slideTimer: Timer;		
		private var _bulkLoader: BulkLoader;		
		[Bindable]
		private var _items: Array;
		
		[Bindable]
		private var _slideIndex: int;
		private var _lastIndex: int;
		private var _totalSlides: uint;
		[Bindable]
		private var _selectedData: GalleryItem;
		
		[Bindable]
		public var bindablePrice: String;
		[Bindable]
		public var bindableAddress: String;
		[Bindable]
		public var bindableDescription: String;
		[Bindable]
		public var thumbPanel: Panel;
		
		public function GalleryCanvas() {
			
		}
		
		private function parseGallery(xml:XML): void {
			var itemList: XMLList = xml.item as XMLList;
			this._items = new Array();
						
			this._bulkLoader = new BulkLoader("mainLoader");
			this._bulkLoader.addEventListener(BulkProgressEvent.COMPLETE, bulkLoaderCompleteHandler);
			
			for each (var xmlItem: XML in itemList) {
				var item: GalleryItem = new GalleryItem(xmlItem);
				this._items.push(item);				
				this._bulkLoader.add(item.photo, { id: item.id } );
			}
			_items.length;
			this._bulkLoader.start();			
		}
		
		public function showThumbnail(ids: String, xPos: Number, yPos: Number): void {
			thumbPanel.addChild(this.createScaledBitmap(this._bulkLoader.getBitmap(ids), 0.1));
			thumbPanel.title = this._items[uint(ids)].price;
			thumbPanel.alpha = 0;
			thumbPanel.visible = true;
			var globalPoint: Point = new Point(xPos, yPos);
			var localPoint: Point = this.globalToLocal(globalPoint);
			thumbPanel.x = localPoint.x - thumbPanel.width / 2 ;
			thumbPanel.y = this.height - thumbPanel.height - 5;
			
			thumbEffect.play();
		}
		
		public function hideThumbnail(): void {
			thumbPanel.visible = false;
			if (thumbPanel.numChildren) {
				thumbPanel.removeChildAt(0);
			}
		}
		
		private function playSlideShow(duration: uint): void {			
			mainEffect.play();
		}
		
		private function createScaledBitmap(originalBitmap: Bitmap, scaleFactor: Number, returnBitmapOnly:Boolean=false): UIComponent {
			var originalBitmapData:BitmapData = originalBitmap.bitmapData;	
			var newWidth:Number = originalBitmapData.width * scaleFactor;
			var newHeight:Number = originalBitmapData.height * scaleFactor;
			var scaledBitmapData:BitmapData = new BitmapData(newWidth, newHeight, true, 0xFFFFFFFF);
			var scaleMatrix:Matrix = new Matrix();

			scaleMatrix.scale(scaleFactor, scaleFactor);
			scaledBitmapData.draw(originalBitmapData, scaleMatrix);
			
			var uiComponent: UIComponent = new UIComponent();
			var newBitmap: Bitmap = new Bitmap(scaledBitmapData);
			uiComponent.addChild(newBitmap);
			return returnBitmapOnly ? newBitmap : uiComponent;
		}
		
		public function fadeInStartHandler(e: EffectEvent): void {
			var refBitmap: Bitmap = this._bulkLoader.getBitmap(this._slideIndex.toString());
			lowerUIComponent.addChild(refBitmap);
			refBitmap.x = (lowerUIComponent.width - refBitmap.width) / 2;
			refBitmap.y = (lowerUIComponent.height - refBitmap.height) / 2;
		}
		
		public function fadeInCompleteHandler(e: EffectEvent): void {
			
			var nextSlideId: uint = this._slideIndex + 1;
			
			if (upperUIComponent.numChildren) {
				upperUIComponent.removeChildAt(0);
				var refBitmap: Bitmap = this._bulkLoader.getBitmap(nextSlideId.toString());
				upperUIComponent.addChild(refBitmap);
				refBitmap.x = (upperUIComponent.width - refBitmap.width) / 2;
				refBitmap.y = (upperUIComponent.height - refBitmap.height) / 2;
			}
		}
		
		private function bulkLoaderCompleteHandler(e:BulkProgressEvent):void {
			this._slideIndex = 0;
			selectedData = this._items[this._slideIndex];
			var refBitmap: Bitmap = this._bulkLoader.getBitmap("0");
			upperUIComponent.addChild(refBitmap);
			refBitmap.x = (this.width - refBitmap.width) / 2;
			refBitmap.y = (this.height - refBitmap.height) / 2;
		}
		
		
		[Bindable]
		public function get xml():XML { return _xml; }
		
		public function set xml(value:XML):void {
			_xml = value;
			
			if (_xml) {
				this.parseGallery(_xml);
			}
		}
		
		public function get slideIndex():int { return _slideIndex; }
		
		public function set slideIndex(value:int):void {
			_slideIndex = value;
			if (this._slideIndex == this._totalSlides) {
				this._slideIndex = 0;
			}
			else if (this._slideIndex < 0) {
				this._slideIndex = this._totalSlides - 1;
			}
			this._lastIndex = this._slideIndex;
			selectedData = this._items[this._slideIndex] as GalleryItem;
			playSlideShow(200);
		}
		
		[Bindable]
		public function get items():Array { return _items; }
		
		public function set items(value:Array):void {
			_items = value;
		}
		
		[Bindable]
		public function get selectedData():GalleryItem { return _selectedData; }
		
		public function set selectedData(value:GalleryItem):void {
			_selectedData = value;
			this.bindableAddress = _selectedData.address;
			this.bindableDescription = _selectedData.description;
			this.bindablePrice = _selectedData.price;
		}
	}
}